package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author moonlan
 * @date 2020/10/18 下午1:07
 */
@Entity
@Table(name = "book")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update book set book_is_deleted = true where book_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "book_is_deleted = false")
@ApiModel("书")
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Book extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "BOOK")})
    @Column(name = "book_id")
    @ApiModelProperty(value = "book的id", position = 1)
    private String bookId;

    @Column(name = "book_name")
    @ApiModelProperty(value = "书名", position = 2)
    private String bookName;

    @Column(name = "book_img_url")
    @ApiModelProperty(value = "书图片", position = 3)
    private String bookImgUrl;

    @Column(name = "book_price")
    @ApiModelProperty(value = "书价格", position = 4)
    private double bookPrice;

    @Column(name = "book_description")
    @ApiModelProperty(value = "书简介", position = 5)
    private String bookDescription;

    @Column(name = "book_is_forbidden")
    @ApiModelProperty(value = "书是否被禁用", position = 6)
    private boolean bookIsForbidden = false;

    @Column(name = "book_is_deleted")
    @ApiModelProperty(value = "书是否被删除", position = 7)
    private boolean bookIsDeleted = false;

    @OneToOne(mappedBy = "book", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private TeacherCourse teacherCourse;

    @OneToOne(mappedBy = "book", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private StudentCourse studentCourse;

    public Book(String bookId, String bookName, String bookImgUrl, double bookPrice, String bookDescription, boolean bookIsForbidden, boolean bookIsDeleted, TeacherCourse teacherCourse, StudentCourse studentCourse) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookImgUrl = bookImgUrl;
        this.bookPrice = bookPrice;
        this.bookDescription = bookDescription;
        this.bookIsForbidden = bookIsForbidden;
        this.bookIsDeleted = bookIsDeleted;
        this.teacherCourse = teacherCourse;
        this.studentCourse = studentCourse;
    }

    public Book() {
    }

    public static BookBuilder builder() {
        return new BookBuilder();
    }

    public String getBookId() {
        return this.bookId;
    }

    public String getBookName() {
        return this.bookName;
    }

    public String getBookImgUrl() {
        return this.bookImgUrl;
    }

    public double getBookPrice() {
        return this.bookPrice;
    }

    public String getBookDescription() {
        return this.bookDescription;
    }

    public boolean isBookIsForbidden() {
        return this.bookIsForbidden;
    }

    public boolean isBookIsDeleted() {
        return this.bookIsDeleted;
    }

    public TeacherCourse getTeacherCourse() {
        return this.teacherCourse;
    }

    public StudentCourse getStudentCourse() {
        return this.studentCourse;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setBookImgUrl(String bookImgUrl) {
        this.bookImgUrl = bookImgUrl;
    }

    public void setBookPrice(double bookPrice) {
        this.bookPrice = bookPrice;
    }

    public void setBookDescription(String bookDescription) {
        this.bookDescription = bookDescription;
    }

    public void setBookIsForbidden(boolean bookIsForbidden) {
        this.bookIsForbidden = bookIsForbidden;
    }

    public void setBookIsDeleted(boolean bookIsDeleted) {
        this.bookIsDeleted = bookIsDeleted;
    }

    public void setTeacherCourse(TeacherCourse teacherCourse) {
        this.teacherCourse = teacherCourse;
    }

    public void setStudentCourse(StudentCourse studentCourse) {
        this.studentCourse = studentCourse;
    }

    public static class BookBuilder {
        private String bookId;
        private String bookName;
        private String bookImgUrl;
        private double bookPrice;
        private String bookDescription;
        private boolean bookIsForbidden;
        private boolean bookIsDeleted;
        private TeacherCourse teacherCourse;
        private StudentCourse studentCourse;

        BookBuilder() {
        }

        public BookBuilder bookId(String bookId) {
            this.bookId = bookId;
            return this;
        }

        public BookBuilder bookName(String bookName) {
            this.bookName = bookName;
            return this;
        }

        public BookBuilder bookImgUrl(String bookImgUrl) {
            this.bookImgUrl = bookImgUrl;
            return this;
        }

        public BookBuilder bookPrice(double bookPrice) {
            this.bookPrice = bookPrice;
            return this;
        }

        public BookBuilder bookDescription(String bookDescription) {
            this.bookDescription = bookDescription;
            return this;
        }

        public BookBuilder bookIsForbidden(boolean bookIsForbidden) {
            this.bookIsForbidden = bookIsForbidden;
            return this;
        }

        public BookBuilder bookIsDeleted(boolean bookIsDeleted) {
            this.bookIsDeleted = bookIsDeleted;
            return this;
        }

        public BookBuilder teacherCourse(TeacherCourse teacherCourse) {
            this.teacherCourse = teacherCourse;
            return this;
        }

        public BookBuilder studentCourse(StudentCourse studentCourse) {
            this.studentCourse = studentCourse;
            return this;
        }

        public Book build() {
            return new Book(bookId, bookName, bookImgUrl, bookPrice, bookDescription, bookIsForbidden, bookIsDeleted, teacherCourse, studentCourse);
        }

        public String toString() {
            return "Book.BookBuilder(bookId=" + this.bookId + ", bookName=" + this.bookName + ", bookImgUrl=" + this.bookImgUrl + ", bookPrice=" + this.bookPrice + ", bookDescription=" + this.bookDescription + ", bookIsForbidden=" + this.bookIsForbidden + ", bookIsDeleted=" + this.bookIsDeleted + ", teacherCourse=" + this.teacherCourse + ", studentCourse=" + this.studentCourse + ")";
        }
    }
}
